#!/usr/bin/env python
#
# This script is meant to produce packages that install files into the default
# user template as well as all other existing home directories. It will _not_
# deploy the files or directories to existing home directories if the given 
# home directory already contains the specified file.
#
# For instance, this script would be ideal for making sure all new and 
# existing users on your computers have Kickass.pdf on their Desktop as it
# would make sure all users have that file on their Desktop as well any new
# users that don't yet have a home directory.
#
# It would _not_ be ideal for deploying bookmarks when the desired effect it
# to overwrite any current bookmarks.
#
# However, the payload will _always_ be installed to the default user template,
# even if the target path already exists.
# 
#
#
import sys
import os
import tempfile
import shutil
import subprocess

if len(sys.argv) < 5:
	sys.stderr.write('Usage: %s <payload> <relative-path> <pkg-name> <version>\n' % (sys.argv[0]))
	sys.stderr.write('       payload: the file or directory to package and deploy\n')
	sys.stderr.write(' relative-path: destination path for the file, relative\n')
	sys.stderr.write('                any user\'s home directory.\n')
	sys.stderr.write('      pkg-name: Desired name of the resulting package.\n')
	sys.stderr.write('       version: Desired version of the resulting package.\n')
	sys.exit(1)



thePayload = sys.argv[1]
theRelativePath = sys.argv[2]
thePkgName = sys.argv[3];
thePkgVersion = sys.argv[4];

if not os.path.exists(thePayload):
	sys.stderr.write("ERROR: Unable to locate '%s'\n" % thePayload)
	sys.exit(1)


tmpPath = tempfile.mkdtemp()
payloadPath = "%s/payload" % tmpPath
scriptsPath = "%s/scripts" % tmpPath
postInstallScript = "%s/scripts/postinstall" % tmpPath


if os.path.isdir(thePayload) == True:
	shutil.copytree(thePayload, payloadPath)
else:
	os.mkdir(payloadPath)
	shutil.copy(thePayload, payloadPath)

os.mkdir(scriptsPath)

f = open(postInstallScript, "w")
f.write("""#!/bin/bash
tplDir="/System/Library/User Template/English.lproj/"
payload="%s/%s"

if [ ! -e "${payload}" ]
then
	payload=`dirname "${payload}"`
fi

payloadTemplate="${tplDir}${payload}"
echo "Will copy ${payloadTemplate} to all homes"

for userdir in /Users/*/
do
	username=`basename "${userdir}"`
	if [ "${username}" = "Shared" ]; then
		continue
	fi

	userPayload="${userdir}${payload}"
	if [ ! -e "${userPayload}" ]; then
		mkdir -p `dirname "${userPayload}"`
		cp -vr "${payloadTemplate}" "${userPayload}"
		chown "${username}" "${userPayload}"
	fi
done
exit 0
""" % (theRelativePath, os.path.basename(thePayload)))
f.close()
os.chmod(postInstallScript, 0777)



pkgPath = "%s/%s.pkg" % (os.getcwd(), thePkgName)
subprocess.call([
	"pkgbuild",
	"--root", payloadPath,
	"--version", thePkgVersion,
	"--install-location", "/System/Library/User Template/English.lproj/%s/" % theRelativePath,
	"--scripts", "%s/scripts" % tmpPath,
	"--identifier", "edu.denison.all-accounts-%s" % thePkgName,
	pkgPath])
shutil.rmtree(tmpPath)

subprocess.call([
	"/usr/local/munki/munkiimport",
	"--catalog=dev",
	"--min-os-ver=10.7",
  "-d", "settings/all-accounts" ,
	pkgPath])

# os.remove(pkgPath)
